package com.woniuxy.controller;

import com.woniuxy.entity.Book;
import com.woniuxy.entity.KillBook;
import com.woniuxy.service.SecKillService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author dingpf
 * @version 1.0
 * @description
 * @create 2025/4/28 14:11
 */
@RestController
@RequestMapping("/seckill")
public class SecKillController {

    @Resource
    private SecKillService secKillService;

    /**
     * 接受书籍信息，放入redis中
     * @return
     */
    @PostMapping("/addBook")
    public ResponseData addSecKillBook(@RequestBody KillBook killBook){
        secKillService.addKillBook(killBook);
        return ResponseData.ok();
    }

    @GetMapping("/queryAll")
    public ResponseData queryAll(){
        List<KillBook> killBooks = secKillService.queryAll();
        return ResponseData.ok(killBooks);
    }

    @GetMapping("/queryDetail/{id}")
    public ResponseData queryDetail(@PathVariable("id") Integer id){
        KillBook killBook = secKillService.queryDetail(id);
        return ResponseData.ok(killBook);
    }

    @GetMapping("/startKill/{id}")
    public ResponseData startKill(@PathVariable("id") Integer id){
        Boolean aBoolean = secKillService.startKill(id);
        if (aBoolean){
            return ResponseData.ok();
        } else {
            return ResponseData.fail(ResponseEnum.SECKILL_END); //提示抢购结束
        }

    }
}
